package  wibrst.visual{
	import flash.display.Loader;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import wibrst.utils.VisualUtil;
	
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class VAuthCodeImg extends Sprite {
		
		static public const EVT_UPDATED:String = "EVT_UPDATED";
		
		private var _vImgCase:Loader;
		private var _spRefresh:Sprite;
		private var _uReqImg:String;
		
		public function VAuthCodeImg( w:Number, h:Number, u:String ):void {
			this._uReqImg = u;
			
			this._vImgCase = new Loader;
			this._vImgCase.contentLoaderInfo.addEventListener( Event.COMPLETE, loadHandler );
			this._vImgCase.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, loadHandler );
			
			this._spRefresh = new Sprite;
			this._spRefresh.useHandCursor = true;
			this._spRefresh.buttonMode = true;
			this._spRefresh.graphics.beginFill( 0xffffff, .0 );
			this._spRefresh.graphics.drawRect(  0, 0, w, h );
			this._spRefresh.graphics.endFill();
			this._spRefresh.addEventListener( MouseEvent.CLICK, refreshHandler );
			
			super.addChild( this._vImgCase );
			super.addChild( this._spRefresh );
		}
		
		public function refresh():void {
			trace( this, "refresh *** ***" );
			this._spRefresh.mouseEnabled = false;
			this._vImgCase.unload();
			this._vImgCase.load( new URLRequest( this._uReqImg ) );
		}
		
		private function loadHandler( evt:Event ):void {
			trace( this, "loaded *** ***", evt.type );
			switch( evt.type ) {
			case Event.COMPLETE:
				this._spRefresh.mouseEnabled = true;
				super.dispatchEvent( new Event( EVT_UPDATED ) );
			break;
			case IOErrorEvent.IO_ERROR:
				//throw new Error( this.toString() );
			break;
			}
		}
		
		private function refreshHandler( evt:MouseEvent ):void {
			trace( this, "refreshHandler *** ***" );
			this.refresh();
		}
		
	}

}